package cn.cdnsu.auth.controller;

import cn.cdnsu.auth.domain.Menu;
import cn.cdnsu.auth.dto.MenuDTO;
import cn.cdnsu.auth.query.MenuQuery;
import cn.cdnsu.auth.service.IMenuServiceI;
import cn.cdnsu.base.util.AjaxResult;
import cn.cdnsu.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuServiceI service;

    @PutMapping
    public AjaxResult queryPage(@RequestBody MenuQuery menuQuery) {
        try {
            PageInfo<Menu> list = service.queryPage(menuQuery);
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping
    public AjaxResult insertOrUpdate(@RequestBody Menu menu){
        try {
            if (Objects.nonNull(menu)){
                if (Objects.nonNull(menu.getId())){
                    service.update(menu);
                }else {
                    service.insert(menu);
                }
                return AjaxResult.success();
            }
            return AjaxResult.error("参数错误");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("menuTree")
    public AjaxResult menuTree(){
        try {
            List<Menu> list=service.menuTree();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/role/{id}")
    public AjaxResult selectByMenuId(@PathVariable("id") Long id) {
        try {
            List<Long> menuIds = service.selectByMenuId(id);
            return AjaxResult.success(menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/role")
    public AjaxResult insertByMenuId(@RequestBody MenuDTO menuDTO) {
        try {
            service.insertByMenuId(menuDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/tree/{id}")
    public AjaxResult queryMenuTree(@PathVariable("id") Long id){
        try {
            List<Menu> list=service.queryMenuTree(id);
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
